<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.10 Bitwise/Logical Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:32 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-10">4.10 Bitwise/Logical Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.BIT1"></A><A NAME="CH04.BIT3"></A>The bitwise/logical
operators in Java are used for bitwise and logical AND (<tt CLASS=literal>&amp;</tt>),
bitwise and logical exclusive OR (<tt CLASS=literal>^</tt>), and bitwise
and logical inclusive OR (<tt CLASS=literal>|</tt>) operations. These
operators have different precedence; the <tt CLASS=literal>&amp;</tt>
operator has the highest precedence of the group and the <tt CLASS=literal>|</tt>
operator has the lowest. All of the operators are evaluated from
left to right.

<P CLASS=para>
The unary operator <tt CLASS=literal>~</tt>
provides a bitwise negation operation.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4.4">Bitwise Negation Operator ~</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-10.1">Bitwise/Logical AND Operator &amp;</A></h3>

<P CLASS=para>
The bitwise/logical AND operator <tt CLASS=literal>&amp;</tt> produces a pure
value that is the AND of its operands. The <tt CLASS=literal>&amp;</tt>
operator may appear in a bitwise or logical AND expression:


<p>
<img align=middle src="./figs/jlr0420.gif" alt="[Graphic: Figure from the text]" width=424 height=74 border=0>

<P CLASS=para>
The
bitwise/logical AND operator is evaluated from left to right. The
operator never throws an exception.

<P CLASS=para>
Here is a code example
that shows the use of the bitwise AND operator:

<DIV CLASS=screen>
<P>
<PRE>
boolean isOdd(int x) {
    return (x &amp; 1) == 1;
}
</PRE>
</DIV>

<P CLASS=para>
The operands
of the bitwise/logical AND operator must both be of either an integer
type or the type <tt CLASS=literal>boolean</tt>, or a compile-time error
occurs.

<P CLASS=para>
If both operands are of integer types, the operator
performs a bitwise AND operation. The operator may perform type
conversions on the operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of
type <tt CLASS=literal>long</tt>, the other operand is converted to
<tt CLASS=literal>long</tt> and the operation produces a <tt CLASS=literal>long</tt>
value.

<P>
<li CLASS=listitem>Otherwise, both operands are converted to <tt CLASS=literal>int</tt>
and the operation produces an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
The bitwise AND operator produces a pure value that is the
bitwise AND of its operands. If the corresponding bits in both of
the converted operands are 1s, the corresponding bit in the result
is a 1; otherwise the corresponding bit in the result is a 0.

<P CLASS=para>
If both operands are of type <tt CLASS=literal>boolean</tt>, the
operator performs a logical AND operation. The logical AND operation
produces a pure value of type <tt CLASS=literal>boolean</tt>. If both
operands are <tt CLASS=literal>true</tt>, the operation produces <tt CLASS=literal>true</tt>;
otherwise the operation produces <tt CLASS=literal>false</tt>. This
operator differs from the conditional AND operator (<tt CLASS=literal>&amp;&amp;</tt>)
because it always evaluates both of its operands, even if its left
operand evaluates to <tt CLASS=literal>false</tt>.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_11.htm#JLR2-CH-4-SECT-11.1">Boolean AND Operator &amp;&amp;</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-10.2">Bitwise/Logical Exclusive OR Operator ^</A></h3>

<P CLASS=para>
The bitwise/logical exclusive OR operator <tt CLASS=literal>^</tt> produces
a pure value that is the exclusive OR of its operands. The <tt CLASS=literal>^</tt>
operator may appear in a bitwise or logical exclusive OR expression:


<p>
<img align=middle src="./figs/jlr0421.gif" alt="[Graphic: Figure from the text]" width=424 height=75 border=0>

<P CLASS=para>
The bitwise/logical exclusive OR operator is evaluated from left to
right. The operator never throws an exception.

<P CLASS=para>
The operands
of the bitwise/logical exclusive OR operator must both be of either
an integer type or the type <tt CLASS=literal>boolean</tt>, or a compile-time
error occurs.

<P CLASS=para>
If both operands are of integer types,
the operator performs a bitwise exclusive OR operation. The operator
may perform type conversions on the operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either
operand is of type <tt CLASS=literal>long</tt>, the other operand is
converted to <tt CLASS=literal>long</tt> and the operation produces
a <tt CLASS=literal>long</tt> value.

<P>
<li CLASS=listitem>Otherwise, both operands
are converted to <tt CLASS=literal>int</tt> and the operation produces
an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
The bitwise exclusive
OR operator produces a pure value that is the bitwise exclusive
OR of its operands. If the corresponding bits in the converted
operands are both 0 or both 1, the corresponding bit in
the result is a 0; otherwise the corresponding bit in the result
is a 1.

<P CLASS=para>
If both operands are of type <tt CLASS=literal>boolean</tt>,
the operator performs a logical exclusive OR operation. The logical
exclusive OR operation produces a pure value of type <tt CLASS=literal>boolean</tt>.
If either, but not both, operands are <tt CLASS=literal>true</tt>, the
operation produces <tt CLASS=literal>true</tt>; otherwise the operation
produces <tt CLASS=literal>false</tt>.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_10.htm#JLR2-CH-4-SECT-10.1">Bitwise/Logical AND Operator &amp;</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-10.3">Bitwise/Logical Inclusive OR Operator |</A></h3>

<P CLASS=para>
The bitwise/logical inclusive OR operator <tt CLASS=literal>|</tt> produces a pure value
that is the inclusive OR of its operands. The <tt CLASS=literal>|</tt>
operator may appear in a bitwise or logical inclusive OR expression:


<p>
<img align=middle src="./figs/jlr0422.gif" alt="[Graphic: Figure from the text]" width=424 height=77 border=0>

<P CLASS=para>
The
bitwise/logical inclusive OR operator is evaluated from left to
right. The operator never throws an exception.

<P CLASS=para>
Here
is a code example that shows the use of the bitwise inclusive OR
operator:

<DIV CLASS=screen>
<P>
<PRE>
setFont("Helvetica", Font.BOLD | Font.ITALIC, 18);
</PRE>
</DIV>

<P CLASS=para>
The operands of the bitwise/logical inclusive OR operator
must both be of either an integer type or the type <tt CLASS=literal>boolean</tt>,
or a compile-time error occurs.

<P CLASS=para>
If both operands are
of integer types, the operator performs a bitwise inclusive OR operation.
The operator may perform type conversions on the operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>long</tt>, the
other operand is converted to <tt CLASS=literal>long</tt> and the operation
produces a <tt CLASS=literal>long</tt> value.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt> and the operation
produces an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
The bitwise
inclusive OR operator produces a pure value that is the bitwise
inclusive OR of its operands. If the corresponding bits in either
or both of the converted operands are 1s, the corresponding bit
in the result is a 1; otherwise the corresponding bit in the result
is a 0.

<P CLASS=para>
If both operands are of type <tt CLASS=literal>boolean</tt>,
the operator performs a logical inclusive OR operation. The logical
inclusive OR operation produces a pure value of type <tt CLASS=literal>boolean</tt>.
If either or both operands are <tt CLASS=literal>true</tt>, the operation
produces <tt CLASS=literal>true</tt>; otherwise the operation produces
<tt CLASS=literal>false</tt>. This operator differs from the conditional
OR operator (<tt CLASS=literal>||</tt>) because it always evaluates
both of its operands, even if its left operand evaluates to <tt CLASS=literal>true</tt>.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_11.htm#JLR2-CH-4-SECT-11.2">Boolean OR Operator ||</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch04_10.htm#JLR2-CH-4-SECT-10.2">Bitwise/Logical Exclusive OR Operator ^</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Equality Comparison Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Boolean Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
